*Get state-day dataset ready.
*Runs analyses.

*NOTE: ado files to do basic data management not provided.

clear
version 16.0
set varabbrev off, permanently
cd C:\Users\User\Downloads\coronavirusstatelaws
makenfile 204uoa_day

*STATE DATA
use 202coronaactions_uoa_state20200330
gen opinion=liberal/conservative
drop school school2 barsrest barsrest2 quar quar2 banbiz banbiz2 sickleave sickleave2 gather gather2 conservative moderate liberal pop18to24 pop25to34 pop35to44 pop45to64 pop65plus cit18to24 cit25to34 cit35to44 cit45to64 cit65plus voted18to24 voted25to34 voted35to44 voted45to64 voted65plus cit65plusper civ2018_18to24 civ2018_25to34 civ2018_35to44 civ2018_45to64 civ2018_65plus
rename (pop65plusper voted65plusper) (pop65 voted65)
save tempstate, replace

*COVID CASES
drop _all
import delimited 010uoastateday_cases20200329reworked.csv
rename *, lower
drop if sab=="GU"|sab=="AS"|sab=="DC"|sab=="MP"|sab=="PR"|sab=="VI"
makestatev
keep sid date cases
assert cases!=.
save $nfile, replace

*MISSING CASES
*Append data for early dates that are missing in the above dataset.
drop _all
import excel 013_uoa_statedate_missing_cases20200329altbyklarner.xlsx, firstrow
keep sid date cases
gen newcase=1
append using $nfile
replace newcase=0 if newcase==.
*Where a state-date already exists in the data, drop it for newcase=1.
bysort sid date (newcase): drop if newcase==1&newcase[1]==0&_N>1
drop newcase

*Confirm there are no duplicate days.
bysort sid date: assert _N<2
*Confirm there are no gaps in day.
count if (date-date[_n-1])==72
assert `r(N)'==50
bysort sid (date): assert (date-date[_n-1])==1 if date[_n-1]!=.&[(date-date[_n-1])!=72]

makestatev

*STATE DATA
merge m:1 state using tempstate
drop if _merge==2
assert _merge==3
drop _merge sfips sab state
erase tempstate.dta

*PER100K
gen per100k=(cases/pop2019)*100000
drop pop2019

save $nfile, replace


*NEIGHBORSTATE
*For each day, take the maximum infection rate in neighbor states.
merge m:1 sid using 009neighborstatelist20200327
assert _merge==3
drop _merge
drop if sid==2|sid==11

reshape long neighborsid, i(sid date) j(col)
drop if neighborsid==.
collapse (max) per100k, by(neighborsid date)
rename (per100k neighborsid) (neighbor sid)
merge 1:1 sid date using $nfile
assert _merge==3 if sid!=2&sid!=11
replace neighbor=0 if sid==2|sid==11
drop _merge

*LAG
*Lag everything by one day by adding "1" to date.
replace date=date+1
replace date=301 if date==230

*WEEKEND
*This should be done after "1" is added to the date.
gen year=2020
gen month=int(date/100)
gen day=mod(date,100)
gen temp=mdy(month,day,year)
replace temp=mod(temp,7)
gen weekend=temp==1|temp==2
drop year month day temp

*TIME
bysort sid (date): gen time=_n-1
gen timesq=time*time
bysort sid date: assert _N<2

save $nfile, replace



*STATE POLICIES
drop _all
import excel 118gubresponses_from_both_116_117_20200330altbyklarner.xlsx, firstrow

local vs soemergency natguard stayathome gather bars school ui childcare eviction

replace date=mod(date,1000)

makestatev
recode `vs' (.=0)
collapse (sum) `vs', by(sid date)
foreach v of local vs {
assert `v'<2
}
merge 1:1 sid date using $nfile

*BARS / GATHER / STAYATHOME
*Make bars and gather = 1 if stayathome=1, and only keep the first "1" to appear.
recode bars gather (*=1) if stayathome==1
bysort sid bars (date): replace bars=0 if bars==1&_n>1
bysort sid gather (date): replace gather=0 if gather==1&_n>1

*If a date appears after a "1," make it system missing.
recode `vs' (.=0)
bysort sid (date): gen row=_n
foreach v of local vs {
gen temp=row if `v'==1
replace temp=0 if temp==.
bysort sid (temp): replace temp=temp[_N]
bysort sid (`v'): gen temp2=`v'[_N]
replace `v'=. if (row>temp)&temp2==1
drop temp temp2
}
replace school=. if school==1&[(sid==5&date==318)|(sid==9&date==317)|(sid==12&date==323)]
replace eviction=. if eviction==1&[(sid==43&date==320)|(sid==31&date==324)]
drop row _merge

makestatev, av(state)
gen month=int(date/100)
gen day=mod(date,100)

*INTER
*Interaction between governor's party and cases.
gen inter=gov*per100k

save $nfile, replace



*FOR APPENDIX B
drop _all
set obs 1
gen temp=999999
save temp, replace
local i 1
foreach v of local vs {
drop _all
use $nfile
keep if `v'==1
keep state month day
sort month day state
gen govaction="`v'"
gen actionnum=`i++'
append using temp
save temp, replace
}

capture log close
makedate
log using "C:\Users\User\Downloads\coronavirusstatelaws\205analysis_output`r(date)'.smcl", replace

*DESCRIPTIVES
drop _all
use $nfile
foreach v of local vs {
di "DESCRIPTIVES OF `v' FOLLOW"
tab `v' gov if date>226&date<327
}
foreach x in gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 time timesq weekend black2018 hisp2018 {
di "DESCRIPTIVES OF `x' FOLLOW"
bysort gov: sum `x' if date>226&date<327
}

*MULTIVARIATE ANALYSES

*CORE MODEL
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
logit `v' i.gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 time timesq weekend black2018 hisp2018 if date>226&date<327
margins gov if date==326, atmeans
predict pred`v'
}
save $nfile, replace


di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX PREDICTED VALUE REPORTS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
foreach v of local vs {
drop _all
use $nfile
keep if date==326
keep state pred`v'
rename pred`v' pred
sort pred state
gen govaction="`v'"
append using temp
save temp, replace
}

di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ROBUSTNESS TESTS XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
*DROP TIME TERM
drop _all
use $nfile
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
logit `v' gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 weekend if date>226&date<327
}

*NOTHING BUT GOVERNOR
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
logit `v' gov if date>226&date<327
}

*INTERACTION BTWN GOV PARTY & CASES PER 100K
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
logit `v' gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 time timesq weekend inter if date>226&date<327
}

*MULTICOLLINEARITY ANALYSIS, INCLUDING INTERACTION
*Don't report time and timesq, as they're going to be highly collinear with each other.
local xs gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 time timesq weekend inter
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Y IS `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
foreach x of local xs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXX X IS `x' XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
local otherxs: list xs - x
quietly reg `x' `otherxs' if `v'!=.&date>226&date<327
local r2=e(r2)
if `r2'>.8&"`x'"!="time"&"`x'"!="timesq" reg `x' `otherxs' if `v'!=.&date>226&date<327
}
}

*MULTICOLLINEARITY ANALYSIS, NO INTERACTION
*Don't report time and timesq, as they're going to be highly collinear with each other.
local xs gov stategdp per100k neighbor pop65 popcon2016 hospitalbeds2018 time timesq weekend
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Y IS `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
foreach x of local xs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXX X IS `x' XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
local otherxs: list xs - x
quietly reg `x' `otherxs' if `v'!=.&date>226&date<327
local r2=e(r2)
if `r2'>.8&"`x'"!="time"&"`x'"!="timesq" reg `x' `otherxs' if `v'!=.&date>226&date<327
}
}

di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF RACE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
foreach v of local vs {
di "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANALYSIS OF `v' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
logit `v' black2018 hisp2018 per100k neighbor popcon2016 time timesq weekend if date>226&date<327
}

log close

*Get party of the governor ready.
drop _all
use 202coronaactions_uoa_state20200330
gen gov2="D" if gov==1
replace gov2="R" if gov==0
keep state gov2
rename gov2 gov
save tempgov, replace

*TABLE WITH PREDICTED PROBABILITIES & DATE OF ADOPTION
drop _all
use temp
drop if temp==999999
drop temp
bysort govaction (actionnum): replace actionnum=actionnum[1] if actionnum==.
replace pred=round(pred*100,.1)
bysort govaction state (month): drop if _n==2&_N==2&month==.&month[1]!=.
merge m:1 govaction using 014gov_action_key20200330
assert _merge==3
drop _merge govaction
rename govaction2 govaction
merge m:1 state using tempgov
drop if _merge==2
assert _merge==3
drop _merge
erase tempgov.dta
order actionnum govaction state gov month day pred
gen temp=pred*-1
sort actionnum month day temp state
drop temp
makedate
export delimited 206gov_actions_for_table`r(date)'.csv, replace

forvalues i=1/9 {
drop _all
makedate
import delimited 206gov_actions_for_table`r(date)'.csv, varnames(1)
keep if actionnum==`i'
drop actionnum
makedate
export delimited 206gov_actions_for_table`r(date)'_`i'.csv, replace
}
